package com.lineage.server.serverpackets;

import com.lineage.server.datatables.ItemPowerUpdateTable;
import com.lineage.server.datatables.ShopCnTable;
import com.lineage.server.datatables.ShopTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1NpcInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1PcInventory;
import com.lineage.server.model.shop.L1Shop;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class S_ShopBuyListAll extends ServerBasePacket {
  private byte[] _byte = null;
  
  public S_ShopBuyListAll(L1PcInstance pc, L1NpcInstance npc) {
    Map<L1ItemInstance, Integer> assessedItems = assessItems(pc.getInventory());
    if (assessedItems.isEmpty()) {
      pc.sendPackets(new S_NoSell(npc));
      return;
    } 
    if (assessedItems.size() <= 0) {
      pc.sendPackets(new S_NoSell(npc));
      return;
    } 
    writeC(65);
    writeD(npc.getId());
    writeH(assessedItems.size());
    Iterator<L1ItemInstance> iterator = assessedItems.keySet().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance key = iterator.next();
      writeD(key.getId());
      writeD(((Integer)assessedItems.get(key)).intValue());
    } 
    writeH(7);
  }
  
  private Map<L1ItemInstance, Integer> assessItems(L1PcInventory inv) {
    Map<L1ItemInstance, Integer> result = new HashMap<>();
    Iterator<L1ItemInstance> iterator = inv.getItems().iterator();
    while (iterator.hasNext()) {
      L1ItemInstance item = iterator.next();
      switch (item.getItem().getItemId()) {
        case 40308:
        case 40314:
        case 40316:
        case 41246:
        case 80033:
        case 83000:
        case 83022:
          continue;
      } 
      if (item.isEquipped())
        continue; 
      int itemid = item.getItemId();
      ArrayList<Integer> cnlist = ShopCnTable.get().get_cnitemidlist();
      if (cnlist.contains(Integer.valueOf(itemid)))
        continue; 
      ArrayList<Integer> uplist = ItemPowerUpdateTable.get().get_updeatitemidlist();
      if (uplist.contains(Integer.valueOf(itemid)))
        continue; 
      boolean contains = ShopCnTable.get().isSelling(110811, itemid);
      if (contains)
        continue; 
      L1Shop shop = ShopTable.get().get(110641);
      if (shop != null) {
        if (shop.isSelling(itemid))
          continue; 
        if (shop.isPurchasing(itemid))
          continue; 
      } 
      L1Shop shop2 = ShopTable.get().get(200206);
      if (shop2 != null) {
        if (shop2.isSelling(itemid))
          continue; 
        if (shop2.isPurchasing(itemid))
          continue; 
      } 
      if (item.getBless() >= 128)
        continue; 
      double price = ShopTable.get().getPrice(itemid);
      double maxchargeCount = item.getItem().getMaxChargeCount();
      double chargecount = item.getChargeCount();
      if (maxchargeCount > 0.0D)
        price *= chargecount / maxchargeCount; 
      if (price > 0.0D)
        result.put(item, new Integer((int)price)); 
    } 
    return result;
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
